#include "stdafx.h"
#include "flydx9.h"
#include "StructureAndConst.h"
#include "BaseDispSprite.h"
#include "DataController.h"
#include "..\LogicSprites\BaseLogicSprite.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

LPD3DXSPRITE BaseDispSprite::m_pD3DSprite=NULL;
LPD3DXFONT BaseDispSprite::m_pStatsFont=NULL;
LPD3DXLINE BaseDispSprite::m_pLine=NULL;
int BaseDispSprite::m_nMapWidth=800;
BaseDispSprite::BaseDispSprite(int nLayer,bool bDrawOnView,bool bCycle)
	:m_nLayer(nLayer),m_pLgSprite(NULL),m_bDrawOnView(bDrawOnView),m_bCycle(bCycle)
{
	m_vCenter.x=0;
	m_vCenter.y=0;
	m_vPosition.x=0;
	m_vPosition.y=0;
}

BaseDispSprite::~BaseDispSprite()
{

}
bool BaseDispSprite::Initialize(LPDIRECT3DDEVICE9 pd3dDevice )
{
	if(S_OK!=D3DXCreateSprite( pd3dDevice, &m_pD3DSprite )){
		AfxMessageBox(_T("D3DXCreateSprite failed"));
		return false;
	}
	if (!m_pD3DSprite){
		AfxMessageBox(_T("D3DXCreateSprite failed"));
		return false;
	}

	TCHAR strFont[LF_FACESIZE]=_T("Arial");
	 if(FAILED(D3DXCreateFont( pd3dDevice,            // D3D device
		   -12,               // Height (-12)
		   0,                     // Width
		   FW_NORMAL,               // Weight
		   1,                     // MipLevels, 0 = autogen mipmaps
		   FALSE,                 // Italic
		   DEFAULT_CHARSET,       // CharSet
		   OUT_DEFAULT_PRECIS,    // OutputPrecision
		   DEFAULT_QUALITY,       // Quality
		   DEFAULT_PITCH | FF_DONTCARE, // PitchAndFamily
		   strFont,              // pFaceName
		   &m_pStatsFont)))// ppFont
	 {              
		AfxMessageBox(_T("D3DXCreateFont failed"));
		return false;
	 }
#if defined(TEST_SFP)||defined(TEST_COLLAPSE)
	 if(FAILED(D3DXCreateLine( pd3dDevice,&m_pLine))){
		 AfxMessageBox(_T("D3DXCreateLine failed"));
		 return false;
	 }
	 m_pLine->SetWidth(3);
#endif
	return true;
}
void BaseDispSprite::Uninitialize()
{
#if defined(TEST_SFP)||defined(TEST_COLLAPSE)
	SAFE_RELEASE(m_pLine)
#endif
	SAFE_RELEASE(m_pStatsFont)
	SAFE_RELEASE(m_pD3DSprite)
}

bool BaseDispSprite::UpdatePosByLgSprite()
{

	if (m_pLgSprite){
		if (!m_pLgSprite->GetShow()){
			return false;
		}
		m_vPosition=m_pLgSprite->GetPos();
		return true;
	}
	return true;
}

#ifdef TEST_COLLAPSE
void BaseDispSprite::DrawLine(D3DXVECTOR2 p1,D3DXVECTOR2 p2)
{
	D3DXVECTOR2 pVertexList[2];
	pVertexList[0]=p1;
	pVertexList[1]=p2;

	m_pLine->Begin();
	m_pLine->Draw(pVertexList,2,D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ));
	m_pLine->End();
}
#endif